<template>
  <div class="register-container">
    <el-form :model="form" :rules="rules" ref="registerForm" label-width="80px" class="register-form">
      <h2 class="title">用户注册</h2>
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="form.userName" auto-complete="off" />
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input v-model="form.nickName" auto-complete="off" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" type="password" auto-complete="off" />
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input v-model="form.confirmPassword" type="password" auto-complete="off" />
      </el-form-item>
      <el-form-item label="手机号" prop="phonenumber">
        <el-input v-model="form.phonenumber" auto-complete="off"  type="phone"/>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" auto-complete="off"  type="email"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleRegister">注册</el-button>
        <el-button @click="$router.push('/login')">返回登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { register } from '@/api/register'
export default {
  name: 'Register',
  data() {
    return {
      form: {
        userName: '',
        nickName: '',
        password: '',
        confirmPassword: '',
        phonenumber: '',
        email:'',
      },
      rules: {
        userName: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        nickName: [
          { required: true, message: '请输入昵称', trigger: 'blur' }
        ],
          email: [{ required: true, message: "邮箱地址不能为空", trigger: "blur" }, { type: "email", message: "请输入正确的邮箱地址", trigger: ["blur", "change"] }],
  phonenumber: [{ required: true, message: "手机号码不能为空", trigger: "blur" }, { pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: "请输入正确的手机号码", trigger: "blur" }],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: (rule, value, callback) => {
              if (value !== this.form.password) {
                callback(new Error('两次输入密码不一致'))
              } else {
                callback()
              }
            }, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (!valid) return
        const data = {
          userName: this.form.userName,
          nickName: this.form.nickName,
          password: this.form.password,
          phonenumber: this.form.phonenumber,
          email: this.form.email
        }
        register(data).then(res => {
          this.$message.success('注册成功，请登录')
          this.$router.push('/login')
        }).catch(err => {
          this.$message.error(err.msg || '注册失败')
        })
      })
    }
  }
}
</script>

<style scoped>
.register-container {
  width: 400px;
  margin: 80px auto;
  padding: 40px 30px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px #eee;
}
.title {
  text-align: center;
  margin-bottom: 30px;
}
</style>
